{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5e985b98",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 数据加密\n",
    "from Cryptodome.Cipher import AES\n",
    "from Cryptodome.Util.Padding import pad\n",
    "import base64\n",
    "\n",
    "\n",
    "def aes_ecb_encrypt(plaintext, key):\n",
    "    cipher = AES.new(key, AES.MODE_ECB)\n",
    "    # 对明文进行pkcs7填充\n",
    "    padded_data = pad(plaintext, AES.block_size, style='pkcs7')\n",
    "    # 加密填充后的明文\n",
    "    ciphertext = cipher.encrypt(padded_data)\n",
    "    return ciphertext\n",
    "\n",
    "\n",
    "while True:\n",
    "    plaintext = input('请输入需要加密的明文（输入1退出）：')\n",
    "    if plaintext == '1':\n",
    "        break\n",
    "    else:\n",
    "        # 将明文转换为bytes类型\n",
    "        plaintext = plaintext.encode()\n",
    "        key = b\"\" # AES加密密钥\n",
    "\n",
    "        # 调用加密函数，并输出密文\n",
    "        ciphertext = aes_ecb_encrypt(plaintext, key)\n",
    "        # Base64编码密文\n",
    "        ciphertext_base64 = base64.b64encode(ciphertext).decode()\n",
    "        print('加密后的结果为：', ciphertext_base64)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13558dda",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 数据解密\n",
    "from Cryptodome.Cipher import AES\n",
    "from Cryptodome.Util.Padding import unpad\n",
    "import base64\n",
    "\n",
    "# 密文和密钥\n",
    "# ciphertext_base64 = \"P3He2aRtmoI1Y6W0RoZRlCmB8HOJURBMUNps9jCD/GwoB8VLuFEC9N+MITughZrL\"\n",
    "\n",
    "def aes_ecb_decrypt(ciphertext, key):\n",
    "    cipher = AES.new(key, AES.MODE_ECB)\n",
    "    plaintext = cipher.decrypt(ciphertext)\n",
    "    return unpad(plaintext, AES.block_size, style='pkcs7')\n",
    "while True:\n",
    "    ciphertext_base64 = input('请输入需要解密的密文（输入1退出）：')\n",
    "    if ciphertext_base64 == '1':\n",
    "        break\n",
    "    else:\n",
    "        key = b\"\" # AES加密密钥\n",
    "\n",
    "        # Base64解码密文\n",
    "        ciphertext = base64.b64decode(ciphertext_base64)\n",
    "\n",
    "        # 调用解密函数，并输出明文\n",
    "        plaintext = aes_ecb_decrypt(ciphertext, key)\n",
    "        print('解密后的结果为：', plaintext.decode().replace('\u0010', '').replace('\\x00', '').replace('\u000E', ''))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "30768407",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
